package org.theonept.travian.t4a.message;

import java.util.List;

import org.theonept.travian.model.Compte;
import org.theonept.travian.model.Message;
import org.theonept.travian.t4a.AbstractFragment;
import org.theonept.travian.t4a.R;
import org.theonept.travian.t4a.R.id;
import org.theonept.travian.t4a.R.layout;
import org.theonept.travian.t4a.R.menu;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

/**
 * A dummy fragment representing a section of the app, but that simply displays
 * dummy text.
 */
public class MessageSectionFragment extends AbstractFragment implements AdapterView.OnItemClickListener {
	/**
	 * The fragment argument representing the section number for this fragment.
	 */
	public static final String ARG_SECTION_NUMBER = "section_number";
	private ListView messageListView;
	private ListMessageAdapter listMessageAdapter;
	private Compte compte;

	public MessageSectionFragment() {

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_compte_message,
				container, false);
		messageListView = (ListView) rootView.findViewById(R.id.listMessage);
		List<Message> messages = (List<Message>) getArguments()
				.getSerializable("messages");
		if (listMessageAdapter == null) {
			listMessageAdapter = new ListMessageAdapter(getActivity());
		}
		this.compte = (Compte) getArguments().getSerializable("compte");
		messageListView.setAdapter(listMessageAdapter);
		if (messages != null) {
			setData(messages);
		}
        messageListView.setOnItemClickListener(this);
		setHasOptionsMenu(true);

		return rootView;
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.compte_message, menu);

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_new_message:
			Intent intentWriteMessage = new Intent(getActivity(),
					WriteMessage.class);
			intentWriteMessage.putExtra("compte", compte);
			startActivity(intentWriteMessage);

			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void setData(List<Message> messages) {
		listMessageAdapter.setData(messages);
		listMessageAdapter.clear();
		listMessageAdapter.addAll(messages);

	}

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        Message message = listMessageAdapter.getItem(position);
        Intent intentMessage = new Intent(getActivity(),ReadMessageActivity.class);
        intentMessage.putExtra("message", message);
        intentMessage.putExtra("compte", compte);
        startActivity(intentMessage);
    }
}